package com.taoban.shopdata.core.impl;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.taoban.shopdata.core.AbstractShopDataConsumer;
import com.taoban.shopdata.entity.AmazonUserInfo;
import com.taoban.shopdata.entity.PriceUpdatedCommitResult;
import com.taoban.shopdata.urlGenerator.ShopDataUrlGenerator;
import com.taoban.shopdata.utils.Constants;
import com.taoban.shopdata.utils.FactoryUtils;
import com.yufei.dataretriver.DataRetrieverFactory;
import com.yufei.dataretriver.DataRetrieverFeatures;
import com.yufei.dataretriver.HttpDataRetriever;
import com.yufei.entity.ProxyServer;
import com.yufei.utils.CommonUtil;
import com.yufei.utils.XPathUtil;



/**
 * @author jasstion
 * 上午11:07:26
 * 用于向server获取需要更行价格的商品id以及根据获取的id利用数据接口进行价格获取然后在将最终的结果发送给server
 * 
 */

public class AmazonShopDataConsumer extends AbstractShopDataConsumer<String,Double>{
	public static final String priceXPath = "//Item";

	
	 HttpDataRetriever dataRetrieverGet = DataRetrieverFactory
				.createDataRetriever(new DataRetrieverFeatures(false, "get"));
	 private AmazonUserInfo amazonUserInfo=null;
public AmazonUserInfo getAmazonUserInfo() {
		return amazonUserInfo;
	}
	public void setAmazonUserInfo(AmazonUserInfo amazonUserInfo) {
		this.amazonUserInfo = amazonUserInfo;
	}
public AmazonShopDataConsumer(PriceUpdatedCommitResult priceUpdatedCommitResult) {
	super();
	this.priceUpdatedCommitResult=priceUpdatedCommitResult;
	// TODO Auto-generated constructor stub
}
public AmazonShopDataConsumer(PriceUpdatedCommitResult priceUpdatedCommitResult,AmazonUserInfo amazonUserInfo) {
	super();
	this.priceUpdatedCommitResult=priceUpdatedCommitResult;
	this.amazonUserInfo=amazonUserInfo;
	// TODO Auto-generated constructor stub
}

	
	
  
	@Override
	public List<Double> getItemPriceByItemInfo(List<String> itemInfo) {
		// TODO Auto-generated method stub
		String requestAmazonUrl=null;
        
		try{
		//requestAmazonUrl=ShopDataUrlGenerator.getAmazonItemLookupUrl(itemInfo);
		
		 
		ProxyServer pro=null;
	    String xmlResponse=CommonUtil.getHtmlContent(pro, false, requestAmazonUrl);
	 
	    List<Double> tempPrices=new ArrayList<Double>();


	    NodeList itemNodeList=XPathUtil.getNodeListtByXPath(xmlResponse, "//Item");
	    Node itemNode=null;
	    String itemPrice=null;
	    String itemAsin=null;
	    for(int i=0;i<itemNodeList.getLength();i++){
	    	itemNode=itemNodeList.item(i);

	    	itemAsin=XPathUtil.getValueOfNode(itemNode, "ASIN");
	    	itemPrice=XPathUtil.getValueOfNode(itemNode, "FormattedPrice");
	    	if(itemPrice!=null){
	    		itemPrice=itemPrice.replaceAll("￥", "").replaceAll(",", "");
				tempPrices.add(Double.parseDouble(itemPrice));
				itemInfos.add(itemAsin);
	    	}
	    	
	    }
	    
	 
		return tempPrices; 
        }
		catch(Exception e){
			mLog.debug("when parse price from the url "+requestAmazonUrl+" error!");
			return null;
		}
	}
	


	@Override
	public void process() {
		// TODO Auto-generated method stub
		try{
			itemInfos= priceUpdatedCommitResult.getItemInfos();
			List<List<String>> itemInfoGroups=CommonUtil.splitKs(itemInfos,10);
			itemInfos.clear();
			prices.clear();
			List<Double> tempPrices=null;
			for(List<String> strs:itemInfoGroups){
			 try {
				Thread.currentThread().sleep(1000*2);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				tempPrices=getItemPriceByItemInfo(strs);
				if(CommonUtil.isEmptyOrNull(tempPrices)){
					continue;
				}
				
				prices.addAll(tempPrices);
			}
			
			
            this.commitItemPrice();
		}catch(Exception e){
			mLog.error(""+e.getMessage()+"");
		}
			
			
			
		
	}
	public static void main(String[] args){
		List<String> itemInfos=new ArrayList();
		itemInfos.add("B003CJSY5E");
		itemInfos.add("B00BJIEPV2");

		
		PriceUpdatedCommitResult priceUpdatedCommitResult=new PriceUpdatedCommitResult(Constants.amazon, itemInfos);
			AbstractShopDataConsumer abstractShopDataConsumer=FactoryUtils.getShopDataConsumer(priceUpdatedCommitResult);
			abstractShopDataConsumer.process();
System.out.print(System.currentTimeMillis());
/*UserInfoList amaInfoList=new UserInfoList();
	AmazonUserInfo amazonUserInfo=new AmazonUserInfo();
	amazonUserInfo.setAssociateTag("on");
	amazonUserInfo.setAWS_ACCESS_KEY_ID("sdfsd");
	amazonUserInfo.setAWS_SECRET_KEY("sfsdf");
	List<AmazonUserInfo> amazonUserInfos=new ArrayList<AmazonUserInfo>();
	amazonUserInfos.add(amazonUserInfo);
	amaInfoList.setUserInfoList(amazonUserInfos);
	System.out.print(CommonUtil.generateXmlForObject(amaInfoList, UserInfoList.class));
	for(int i=0;i<100;i++){
		System.out.print(new Random().nextInt(4));
	}*/
	
	}


}
